using System;
using Volo.Abp.Domain.Entities.Auditing;
using Volo.Abp.MultiTenancy;

namespace Matrix.Configuration
{
    /// <summary>
    /// 配置项实体
    /// </summary>
    public class ConfigurationItem : FullAuditedAggregateRoot<Guid>, IMultiTenant
    {
        /// <summary>
        /// 命名空间
        /// </summary>
        public string Namespace { get; private set; } = string.Empty;

        /// <summary>
        /// 配置键名
        /// </summary>
        public string Key { get; private set; } = string.Empty;

        /// <summary>
        /// 配置值
        /// </summary>
        public string Value { get; private set; } = string.Empty;

        /// <summary>
        /// 数据类型
        /// </summary>
        public string DataType { get; private set; } = string.Empty;

        /// <summary>
        /// 配置分类
        /// </summary>
        public string Category { get; private set; } = string.Empty;

        /// <summary>
        /// 配置描述
        /// </summary>
        public string Description { get; private set; } = string.Empty;

        /// <summary>
        /// 配置版本
        /// </summary>
        public long Version { get; private set; }

        /// <summary>
        /// 租户标识
        /// </summary>
        public Guid? TenantId { get; set; }

        protected ConfigurationItem()
        {
        }

        public ConfigurationItem(
            Guid id,
            string @namespace,
            string key,
            string value,
            string dataType,
            string category = "",
            string? description = null)
            : base(id)
        {
            Namespace = @namespace;
            Key = key;
            SetValue(value, dataType);
            Category = category;
            Description = description ?? string.Empty;
            Version = 1;
        }

        /// <summary>
        /// 设置配置值
        /// </summary>
        /// <param name="value">配置值</param>
        /// <param name="dataType">数据类型</param>
        public void SetValue(string value, string dataType)
        {
            if (string.IsNullOrWhiteSpace(value))
                throw new ArgumentException("配置值不能为空", nameof(value));

            if (string.IsNullOrWhiteSpace(dataType))
                throw new ArgumentException("数据类型不能为空", nameof(dataType));

            Value = value;
            DataType = dataType;
            Version++;
        }

        /// <summary>
        /// 更新描述
        /// </summary>
        /// <param name="description">描述</param>
        public void UpdateDescription(string? description)
        {
            Description = description ?? string.Empty;
        }

        /// <summary>
        /// 设置分类
        /// </summary>
        /// <param name="category">分类</param>
        public void SetCategory(string category)
        {
            Category = category ?? string.Empty;
        }

        /// <summary>
        /// 设置描述
        /// </summary>
        /// <param name="description">描述</param>
        public void SetDescription(string? description)
        {
            Description = description ?? string.Empty;
        }

        /// <summary>
        /// 设置数据类型
        /// </summary>
        /// <param name="dataType">数据类型</param>
        public void SetDataType(string dataType)
        {
            DataType = dataType ?? "string";
        }

        public override string ToString()
        {
            return $"{Namespace}:{Key} = {Value} ({DataType})";
        }
    }
}